/**
 * @Author : JiGuangJie
 * @Date : 2023/1/30
 * @Time : 下午2:02
 * @Version : 1.0.0
 * @description :
 */
import {GisPlot, PlotTextBox, PlotTypes} from '../../../../src';
import BasePlot from '../BasePlot';

export default class Offline extends BasePlot {
    constructor(gisPlot: GisPlot, plotLayer: any, map: any) {
        super(gisPlot, plotLayer, map);
        this._gisPlot.plotDraw.addListener('drawEnd', this.drawEndHandler);
        this._gisPlot.plotEdit.addListener('deleteComplete', this.deleteCompleteHandler);
        this._gisPlot.plotEdit.addListener('scaleComplete', this.scaleCompleteHandler);
        this._gisPlot.plotEdit.addListener('translateComplete', this.translateCompleteHandler);
        this._gisPlot.plotEdit.addListener('rotateComplete', this.rotateCompleteHandler);
    }

    drawEndHandler = (event: any): void => {
        if (event.plotType && event.plotType === 'text') { // 文本
            const {width, height}: { width: number, height: number } = event.data;
            const plotText: PlotTextBox = new PlotTextBox({
                id: event.data.id,
                center: event.data.center,
                value: 'test',
                width,
                height,
                style: event.data.style
            });
            this._map.addOverlay(plotText);
            plotText.onMove = (position: any): void => {
                (plotText as any).setPosition(position);
            };
            plotText.onDblClick = (): void => {
                console.log('=======双击');
                this._map.removeOverlay(plotText);
            };
        } else {
            const {plotType}: { plotType: string } = event.feature.getProperties();
            let geometry: any = null;
            if (plotType === 'point') {
                geometry = this._gisPlot.plotDraw.createPlot(plotType as PlotTypes, [event.data.point], {});
            } else {
                geometry = this._gisPlot.plotDraw.createPlot(plotType as PlotTypes, event.data.path, event.data.style);
            }
            const feature = new ol.Feature(geometry);
            feature.set('isPlot', true);
            feature.set('plotType', plotType);
            const style: any = this._gisPlot.plotDraw.createStyle(plotType as PlotTypes, event.data.style);
            feature.setStyle(style);
            this._plotLayer.getSource().addFeature(feature);
        }
    };

    deleteCompleteHandler = (event: any): void => {
        this._plotLayer.getSource().removeFeature(event.feature);
    };

    scaleCompleteHandler = (event: any): void => {
        console.log('scaleCompleteHandler');
    };

    translateCompleteHandler = (event: any): void => {
        console.log('translateCompleteHandler');
    };

    rotateCompleteHandler = (event: any): void => {
        console.log('rotateCompleteHandler');
    };
}
